package itbour.onetouchshow.bean.inching.noppt;

import java.util.ArrayList;
import java.util.List;

import itbour.onetouchshow.constants.AppConst;
import itbour.onetouchshow.singlecase.InchingDataSingleCase;
import itbour.onetouchshow.utils.FineTuneRender;
import itbour.onetouchshow.utils.L_;

/**
 * Created by zjl_d on 2017/11/22.
 * 本类用于非ppt的核心结构 包括竖屏和视频背景
 */

public class InchingCoreRootNoPPTV10Bean {
    /**
     * SJGTGNodeModelKindRoot      = 0,    // 根节点
     * SJGTGNodeModelKindArray     = 1,    // 数组节点
     * SJGTGNodeModelKindStructure = 2,    // 结构体节点
     * SJGTGNodeModelKindLeaf      = 3     // 叶节点
     */
    public static int SJGNodeKindRoot = 0;
    public static int SJGNodeKindArray = 1;
    public static int SJGNodeKindStructure = 2;
    public static int SJGNodeKindLeaf = 3;

    /**
     * SJGTGNodeModeTypeNone   = 0,    // 无
     * SJGTGNodeModeTypeText   = 1,    // 文字
     * SJGTGNodeModeTypeImage  = 2,    // 图片
     * SJGTGNodeModeTypeGraph  = 3,    // 图表
     * SJGTGNodeModeTypeTable  = 4,    // 表格
     * SJGTGNodeModeTypeShape  = 5,    // 图形
     */
    public static int SJGNodeTypeNone = 0;
    public static int SJGNodeTypeText = 1;
    public static int SJGNodeTypeImage = 2;
    public static int SJGNodeTypeGraph = 3;
    public static int SJGNodeTypeTable = 4;
    public static int SJGTGNodeModeTypeShape = 5;
    public static int SJGNodeTypeVideo = 7;

    public Integer ct;
    //id用String是因为有很大的数字超过了int的最大范围
    public Long id;
    public String topic;
    public Integer type;
    public Integer kind;

    //child的结构自己
    public List<InchingCoreRootNoPPTV10Bean> children;

    /**
     * 如果是数据版本为1，2 page_index和obj_id在外面
     * 否则在relations里
     */
    public Integer page_index;
    public Integer obj_id;


    //这里要根据type来封装 用Object
    public Object value;
    /**
     * 版本号大于2 标记不同页面的同一个对象
     */
    public List<InchingRelationsV10Bean> relations;

    public Integer child_max;

    /**
     * 如果h=1 不显示 如果p=1 不显示
     * 如果
     * o= 0基本信息
     * =1 可选(补充信息)
     * =2 自由
     */

    public int h;//number=0;  1表示hidden
    public int o; //number=0;  1表示optional(可选项)
    public int p; //number=0;  1表示preserve(保留项)

    //据说这个版本没用 先写上
    public InchingCoreRootNoPPTV10Bean child_template;

    public InchingCoreRootNoPPTV10Bean getChild_template() {
        return child_template;
    }

    public void setChild_template(InchingCoreRootNoPPTV10Bean child_template) {
        this.child_template = child_template;
    }

    public Integer getCt() {
        return ct;
    }

    public void setCt(Integer ct) {
        this.ct = ct;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getTopic() {
        return topic;
    }

    public void setTopic(String topic) {
        this.topic = topic;
    }

    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }

    public Integer getKind() {
        return kind;
    }

    public void setKind(Integer kind) {
        this.kind = kind;
    }

    public List<InchingCoreRootNoPPTV10Bean> getChildren() {
        return children;
    }

    public void setChildren(List<InchingCoreRootNoPPTV10Bean> children) {
        this.children = children;
    }

    public Integer getPage_index() {
        return page_index;
    }

    public void setPage_index(Integer page_index) {
        this.page_index = page_index;
    }

    public Integer getObj_id() {
        return obj_id;
    }

    public void setObj_id(Integer obj_id) {
        this.obj_id = obj_id;
    }

    public Object getValue() {
        return value;
    }

    public void setValue(Object value) {
        this.value = value;
    }

    public List<InchingRelationsV10Bean> getRelations() {
        return relations;
    }

    public void setRelations(List<InchingRelationsV10Bean> relations) {
        this.relations = relations;
    }

    public Integer getChild_max() {
        return child_max;
    }

    public void setChild_max(Integer child_max) {
        this.child_max = child_max;
    }

    public int getH() {
        return h;
    }

    public void setH(int h) {
        this.h = h;
    }

    public int getO() {
        return o;
    }

    public void setO(int o) {
        this.o = o;
    }

    public int getP() {
        return p;
    }

    public void setP(int p) {
        this.p = p;
    }


    /**
     * 根据相应的id获取text对象的ValueBean
     *
     * @param nowSelectId
     * @return
     */
    public NoPPTRootTextValueV10Bean getTextBeanTypeFromId(int nowSelectId, int version) {
        List<InchingCoreRootNoPPTV10Bean> children = this.getChildren();
        if (children != null && children.size() != 0) {
            for (int i = 0; i < children.size(); i++) {
                InchingCoreRootNoPPTV10Bean dataCoreStructureRootBean = children.get(i);
                if (version >= AppConst.SUPPROT_RELATIONSLIST_VERSION) {
                    List<InchingRelationsV10Bean> relations = dataCoreStructureRootBean.getRelations();
                    if (relations != null && relations.size() != 0) {
                        for (int relationsCount = 0; relationsCount < relations.size(); relationsCount++) {
                            InchingRelationsV10Bean relationsBean = relations.get(relationsCount);
                            Integer objId = relationsBean.getObj_id();
                            if (nowSelectId == objId && dataCoreStructureRootBean.type.equals(SJGNodeTypeText)) {
                                NoPPTRootTextValueV10Bean value = (NoPPTRootTextValueV10Bean) dataCoreStructureRootBean.getValue();
                                return value;
                            }
                        }
                    }
                } else {
                    Integer objId = dataCoreStructureRootBean.getObj_id();
                    if (nowSelectId == objId && dataCoreStructureRootBean.type.equals(SJGNodeTypeText)) {
                        NoPPTRootTextValueV10Bean value = (NoPPTRootTextValueV10Bean) dataCoreStructureRootBean.getValue();
                        return value;
                    }
                }
            }

        }
        return null;
    }

    /**
     * 根据相应的id获取text对象的ValueBean
     * 这是微调数据
     *
     * @param nowSelectId
     * @return
     */
    public NoPPTRootTableValueV10Bean getTableBeanTypeFromId(int nowSelectId, int version) {
        List<InchingCoreRootNoPPTV10Bean> children = this.getChildren();
        if (children != null && children.size() != 0) {
            for (int i = 0; i < children.size(); i++) {
                InchingCoreRootNoPPTV10Bean dataCoreStructureRootBean = children.get(i);
                if (version >= AppConst.SUPPROT_RELATIONSLIST_VERSION) {
                    List<InchingRelationsV10Bean> relations = dataCoreStructureRootBean.getRelations();
                    if (relations != null && relations.size() != 0) {
                        for (int relationsCount = 0; relationsCount < relations.size(); relationsCount++) {
                            InchingRelationsV10Bean relationsBean = relations.get(relationsCount);
                            Integer objId = relationsBean.getObj_id();
                            if (nowSelectId == objId && dataCoreStructureRootBean.type.equals(SJGNodeTypeTable)) {
                                NoPPTRootTableValueV10Bean value = (NoPPTRootTableValueV10Bean) dataCoreStructureRootBean.getValue();
                                return value;
                            }
                        }
                    }
                } else {
                    Integer objId = dataCoreStructureRootBean.getObj_id();
                    if (nowSelectId == objId && dataCoreStructureRootBean.type.equals(SJGNodeTypeTable)) {
                        NoPPTRootTableValueV10Bean value = (NoPPTRootTableValueV10Bean) dataCoreStructureRootBean.getValue();
                        return value;
                    }
                }
            }

        }
        return null;
    }

    public InchingCoreRootNoPPTV10Bean getTableBeanCoreRootTypeFromId(int nowSelectId, int version) {
        List<InchingCoreRootNoPPTV10Bean> children = this.getChildren();
        if (children != null && children.size() != 0) {
            for (int i = 0; i < children.size(); i++) {
                InchingCoreRootNoPPTV10Bean dataCoreStructureRootBean = children.get(i);
                if (version >= AppConst.SUPPROT_RELATIONSLIST_VERSION) {
                    List<InchingRelationsV10Bean> relations = dataCoreStructureRootBean.getRelations();
                    if (relations != null && relations.size() != 0) {
                        for (int relationsCount = 0; relationsCount < relations.size(); relationsCount++) {
                            InchingRelationsV10Bean relationsBean = relations.get(relationsCount);
                            Integer objId = relationsBean.getObj_id();
                            if (nowSelectId == objId && dataCoreStructureRootBean.type.equals(SJGNodeTypeTable)) {
                                return dataCoreStructureRootBean;
                            }
                        }
                    }
                } else {
                    Integer objId = dataCoreStructureRootBean.getObj_id();
                    if (nowSelectId == objId && dataCoreStructureRootBean.type.equals(SJGNodeTypeTable)) {
                        return dataCoreStructureRootBean;
                    }
                }
            }

        }
        return null;
    }

    /**
     * 刷新数据的值
     * 修改文字 图片
     *
     * @param fId      这里的id是objId FineTuneRenderId
     * @param outValue
     */
    public void upDateAppointValue(int fId, String outValue) {

        //如果传入的pageIndex 和obj_id相等  那么清空该节点的值
        InchingDocStringNoPPTV10Bean instance = InchingDataSingleCase.INSTANCE.getInstance();
        int version = instance.getCoreStructure().getVersion();
        if (version >= AppConst.SUPPROT_RELATIONSLIST_VERSION) {


            List<InchingRelationsV10Bean> relations = this.getRelations();
            if (relations != null && relations.size() > 0) {
                for (int i = 0; i < relations.size(); i++) {
                    //微调库移除值的方式是在rela里
                    Integer obj_id = relations.get(i).getObj_id();
                    Integer page_index = relations.get(i).getPage_index();
                    L_.i("version>=3===" + "obj_id===" + obj_id + "page_index===" + page_index);
                    if (obj_id == fId) {
                        if (this.type.equals(SJGNodeTypeText)) {
//                            o = 2 h=0 p=0
                            NoPPTRootTextValueV10Bean text = (NoPPTRootTextValueV10Bean) this.value;
                            text.value = outValue;
                            FineTuneRender.changeTextContentForJava(page_index, fId, outValue);
                        } else if (this.type.equals(SJGNodeTypeImage)) {
                            //v520原有逻辑
                            NoPPTRootImageValueV10Bean image = (NoPPTRootImageValueV10Bean) this.value;
                            int w = (int) image.getPos().getW();
                            int h = (int) image.getPos().getH();
                            image.value = outValue;
                            FineTuneRender.changeImageContentForJava(page_index, fId, outValue, w, h);
                        } else if (this.type.equals(SJGNodeTypeTable)) {
                        }
                    }
                }
            }
        } else {


            //微调库移除值的方式是直接获取
            Integer page_index = this.getPage_index();
            Integer obj_id = this.getObj_id();

            if (page_index != null && obj_id != null) {


                L_.i("version<3===" + "obj_id===" + obj_id + "page_index===" + page_index);
                if (obj_id == fId) {
                    if (this.type.equals(SJGNodeTypeText)) {
                        NoPPTRootTextValueV10Bean text = (NoPPTRootTextValueV10Bean) this.value;
                        text.value = outValue;
                        FineTuneRender.changeTextContentForJava(page_index, fId, outValue);
                    } else if (this.type.equals(SJGNodeTypeImage)) {
                        //v520原有逻辑
                        NoPPTRootImageValueV10Bean image = (NoPPTRootImageValueV10Bean) this.value;
                        int w = (int) image.getPos().getW();
                        int h = (int) image.getPos().getH();
                        image.value = outValue;
                        FineTuneRender.changeImageContentForJava(page_index, fId, outValue, w, h);
                    }
                }
            }
        }


        // 更新子节点默认值
        if (this.children != null) {
            for (InchingCoreRootNoPPTV10Bean node : this.children) {
                node.upDateAppointValue(fId, outValue);
            }
        }
    }


    /**
     * @param fId 符合该id的 删除所有页面的该对象
     *            <p>
     *            这里用于记录
     */
    public void recoredNeedDelAppointValue(int fId) {

        //如果传入的pageIndex 和obj_id相等  那么清空该节点的值
        InchingDocStringNoPPTV10Bean instance = InchingDataSingleCase.INSTANCE.getInstance();
        int version = instance.getCoreStructure().getVersion();
        if (version >= AppConst.SUPPROT_RELATIONSLIST_VERSION) {
            List<InchingRelationsV10Bean> relations = this.getRelations();
            if (relations != null && relations.size() > 0) {


                for (int i = 0; i < relations.size(); i++) {
                    //微调库移除值的方式是在rela里
                    Integer obj_id = relations.get(i).getObj_id();
                    Integer page_index = relations.get(i).getPage_index();
                    if (obj_id == fId) {
                        InchingDataSingleCase.INSTANCE.getDeleteList().add(this);
                        if (this.type.equals(SJGNodeTypeText)) {

                            //同时清除微调库中的值
                            FineTuneRender.deleteObjectForJava(page_index, obj_id);


                        } else if (this.type.equals(SJGNodeTypeImage)) {

                            //同时清除微调库中的值
                            FineTuneRender.deleteObjectForJava(page_index, obj_id);


                        } else if (this.type.equals(SJGNodeTypeTable)) {

                            NoPPTRootTableValueV10Bean value = (NoPPTRootTableValueV10Bean) this.value;
                            //用于table中的objIdlist
                            FineTuneRender.deleteObjectForJava(page_index, obj_id);

//                            List<NoPPTRootTableValueV10Bean.RootTableValueV10RowBean> rawData = value.children;
//                            int rawSize = rawData.size();
//
//                            for (int rawcCount = 0; rawcCount < rawSize; rawcCount++) {
//                                List<NoPPTRootTableValueV10Bean.RootTableValueV10RowBean.RootTableValueV10ColumnBean> cellData = rawData.get(rawcCount).children;
//                                int cellSize = cellData.size();
//                                for (int cellCout = 0; cellCout < cellSize; cellCout++) {
//
//                                    NoPPTRootTableValueV10Bean.RootTableValueV10RowBean.RootTableValueV10ColumnBean designOrPrintTableBeanV54Column = cellData.get(cellCout);
//
//                                    List<NoPPTRootTextValueV10Bean> text = designOrPrintTableBeanV54Column.children;
//                                    NoPPTRootTextValueV10Bean quickEditActRootValueText = text.get(0);
//                                    ArrayList<ArrayList<ArrayList<Integer>>> arrayLists = transTableTextValueToArrayList(tableCellChildrenIdForJava, rawSize, cellSize);
//
//                                    Integer tableTextObjid = arrayLists.get(rawcCount).get(cellCout).get(0);
//                                    quickEditActRootValueText.pageIndexAndObjIdList.add(new InchingRelationsV10Bean(page_index, tableTextObjid));
//                                    quickEditActRootValueText.value = "";
//
//                                    FineTuneRender.clearOneObjectContentForJava(page_index, tableTextObjid);
//                                }
//                            }
                        }
                    }
                }
            }
        } else {
            Integer page_index = this.getPage_index();
            Integer obj_id = this.getObj_id();
            if (page_index != null && obj_id != null) {
                if (obj_id == fId) {
                    if (this.type.equals(SJGNodeTypeText)) {
                        //同时清除微调库中的值
                        FineTuneRender.deleteObjectForJava(page_index, fId);
                    } else if (this.type.equals(SJGNodeTypeImage)) {
                        //同时清除微调库中的值
                        FineTuneRender.deleteObjectForJava(page_index, fId);
                    }

                    InchingDataSingleCase.INSTANCE.getDeleteList().add(this);
                }
            }

        }
        if (this.children != null) {
            // 移除子节点默认值
            for (InchingCoreRootNoPPTV10Bean node : this.children) {
                node.recoredNeedDelAppointValue(fId);
            }
        }

    }

    /**
     * 删除时删除记录的节点
     */
    public void delNode() {
        ArrayList<InchingCoreRootNoPPTV10Bean> deleteList = InchingDataSingleCase.INSTANCE.getDeleteList();
        if (deleteList != null && deleteList.size() != 0) {
            for (InchingCoreRootNoPPTV10Bean node : deleteList) {
                this.children.remove(node);
            }
            deleteList.clear();
        }
    }

    /**
     * 转化tablevalue为arrayList
     *
     * @param data
     * @param rawSize
     * @param cellSize
     * @return
     */
    public ArrayList<ArrayList<ArrayList<Integer>>> transTableTextValueToArrayList(String data, int rawSize, int cellSize) {
        String replace = data.replace("[", "");
        String replace1 = replace.replace("]", "");
        String[] split = replace1.split(",");
        ArrayList<ArrayList<ArrayList<Integer>>> firstList = new ArrayList<>();

        for (int rawCount = 0; rawCount < rawSize; rawCount++) {
            ArrayList<ArrayList<Integer>> secendLists = new ArrayList<>();
            firstList.add(secendLists);
            for (int cellCount = 0; cellCount < cellSize; cellCount++) {
                ArrayList<Integer> thridList = new ArrayList<>(1);
                secendLists.add(thridList);
            }
        }


        for (int i = 0; i < split.length; i++) {
            int i1 = i / cellSize;
            int i2 = i % cellSize;
            if (i1 <= (rawSize - 1) && i2 <= (cellSize - 1)) {
                firstList.get(i1).get(i2).add(0, Integer.parseInt(split[i]));
            }

        }
        return firstList;
    }

    /**
     * 传入最大coreRootData下一级的数据 获取pageIndexList和objIdList
     *
     * @param dataCoreStructure
     * @param tag
     * @return
     */
    public ArrayList<Integer> getPageIndexOrObjIdList(InchingCoreRootNoPPTV10Bean dataCoreStructure, Integer tag, int version) {
        ArrayList<Integer> list = new ArrayList<>();
        List<InchingRelationsV10Bean> relations = dataCoreStructure.getRelations();
        Integer id = -1;
        //这是支持relation是数组的情况
        if (version >= AppConst.SUPPROT_RELATIONSLIST_VERSION) {
            for (int i = 0; i < relations.size(); i++) {
                if (tag == AppConst.PAGEINDEXTAG) {
                    id = relations.get(i).getPage_index();
                }
                if (tag == AppConst.OBJIDTAG) {
                    id = relations.get(i).getObj_id();
                }
                list.add(id);
            }
        }//为12
        else {
            if (tag == AppConst.PAGEINDEXTAG) {
                id = dataCoreStructure.getPage_index();
            }
            if (tag == AppConst.OBJIDTAG) {
                id = dataCoreStructure.getObj_id();
            }
            list.add(id);

        }

        return list;
    }

    /**
     * 本方法用于刷新表格的数组
     */
//    public void refreshTableData() {
//        InchingDocStringNoPPTV10Bean instance = InchingDataSingleCase.INSTANCE.getInstance();
//        int version = instance.getCoreStructure().getVersion();
//        if (version >= 3) {
//            List<InchingRelationsV10Bean> relations = this.getRelations();
//            if (relations != null && relations.size() > 0) {
//                for (int i = 0; i < relations.size(); i++) {
//                    //微调库移除值的方式是在rela里
//                    Integer obj_id = relations.get(i).getObj_id();
//                    Integer page_index = relations.get(i).getPage_index();
//                    if (this.type.equals(SJGNodeTypeTable)) {
//
//                        NoPPTRootTableValueV10Bean value = (NoPPTRootTableValueV10Bean) this.value;
//                        //用于table中的objIdlist
////                        String tableCellChildrenIdForJava = FineTuneRender.getTableCellChildrenIdForJava(page_index, obj_id);
//
//                        List<NoPPTRootTableValueV10Bean.RootTableValueV10RowBean> rawData = value.children;
//                        int rawSize = rawData.size();
//
//                        for (int rawcCount = 0; rawcCount < rawSize; rawcCount++) {
//                            int cellSize = rawData.get(rawcCount).children.size();
//                            List<NoPPTRootTableValueV10Bean.RootTableValueV10RowBean.RootTableValueV10ColumnBean> cellData
//                                    = rawData.get(rawcCount).children;
//                            for (int cellCout = 0; cellCout < cellSize; cellCout++) {
//
//                                NoPPTRootTableValueV10Bean.RootTableValueV10RowBean.RootTableValueV10ColumnBean designOrPrintTableBeanV54Column = cellData.get(cellCout);
//
//                                List<NoPPTRootTextValueV10Bean> text = designOrPrintTableBeanV54Column.children;
//                                NoPPTRootTextValueV10Bean quickEditActRootValueText = text.get(0);
////                                ArrayList<ArrayList<ArrayList<Integer>>> arrayLists = transTableTextValueToArrayList(tableCellChildrenIdForJava, rawSize, cellSize);
//
//                                Integer tableTextObjid = arrayLists.get(rawcCount).get(cellCout).get(0);
//                                int size = quickEditActRootValueText.pageIndexAndObjIdList.size();
//                                if (size != 0 && size > i) {
//                                    quickEditActRootValueText.pageIndexAndObjIdList.set(i, new InchingRelationsV10Bean(page_index, tableTextObjid));
//
//                                } else {
//                                    quickEditActRootValueText.pageIndexAndObjIdList.add(new InchingRelationsV10Bean(page_index, tableTextObjid));
//                                }
//
//                                FineTuneRender.changeTableTextForJava(page_index, tableTextObjid,rawcCount,cellCout, quickEditActRootValueText.value);
//                            }
//                        }
//                    }
//                }
//            }
//        }
//    }

    /**
     * 通过id获取大纲imgBean
     *
     * @param nowSelectId
     * @param version
     * @return
     */
    public InchingCoreRootNoPPTV10Bean getImgBeanCoreRootTypeFromId(int nowSelectId, int version) {
        List<InchingCoreRootNoPPTV10Bean> children = this.getChildren();
        if (children != null && children.size() != 0) {
            for (int i = 0; i < children.size(); i++) {
                InchingCoreRootNoPPTV10Bean dataCoreStructureRootBean = children.get(i);
                if (version >= AppConst.SUPPROT_RELATIONSLIST_VERSION) {
                    List<InchingRelationsV10Bean> relations = dataCoreStructureRootBean.getRelations();
                    if (relations != null && relations.size() != 0) {
                        for (int relationsCount = 0; relationsCount < relations.size(); relationsCount++) {
                            InchingRelationsV10Bean relationsBean = relations.get(relationsCount);
                            Integer objId = relationsBean.getObj_id();
                            if (nowSelectId == objId && dataCoreStructureRootBean.type.equals(SJGNodeTypeImage)) {
                                return dataCoreStructureRootBean;
                            }
                        }
                    }
                } else {
                    Integer objId = dataCoreStructureRootBean.getObj_id();
                    if (nowSelectId == objId && dataCoreStructureRootBean.type.equals(SJGNodeTypeImage)) {
                        return dataCoreStructureRootBean;
                    }
                }
            }

        }
        return null;
    }


}
